package com.atguigu.eduservice.controller;


import com.atguigu.common.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.tree.TreeEntity;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-01-06
 */
@Api(tags = {"章节管理"})
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "课程大纲列表")
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<TreeEntity> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        try {
            chapterService.save(eduChapter);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("根据章节id查询")
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        return R.ok().data("chapter", chapterService.getById(chapterId));
    }

    @ApiOperation("修改章节")
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        try {
            chapterService.updateById(eduChapter);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("删除章节")
    @DeleteMapping("/delete/{chapterId}")
    public R delete(@PathVariable String chapterId) {
        try {
            return chapterService.deleteChapter(chapterId) ? R.ok() : R.error();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


}
